package com.nvn.washiodriver.adapters;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nvn.washiodriver.R;
import com.nvn.washiodriver.Objects.Order;

public class OrderListAdapter extends BaseAdapter implements OnClickListener{

	private ArrayList<Order> items;
	private LayoutInflater mInflater;

	public OrderListAdapter(Context mContext, ArrayList<Order> items) {
		this.items = items;
		mInflater = (LayoutInflater) mContext
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final Order o = getItem(position);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_list_order, null);
		}

		TextView tvTime = (TextView) convertView.findViewById(R.id.tv_detail_time);
		TextView tvType = (TextView) convertView.findViewById(R.id.tv_order_type);
		TextView tvCustomer = (TextView) convertView.findViewById(R.id.tv_customer_name);
		TextView tvAddress = (TextView) convertView.findViewById(R.id.tv_address);
		TextView tvPhoneZip = (TextView) convertView.findViewById(R.id.tv_phone_zip);
		TextView tvStatus = (TextView) convertView.findViewById(R.id.tv_status);
		
		TextView tvNote = (TextView)convertView.findViewById(R.id.tv_notes);
		TextView tvNew = (TextView)convertView.findViewById(R.id.tv_new);
		TextView tvVip = (TextView)convertView.findViewById(R.id.tv_vip);
		TextView tvBags = (TextView)convertView.findViewById(R.id.tv_bag);
		
		tvTime.setText(o.getTime());
		tvType.setText(o.getServiceType());
		tvCustomer.setText(o.getCustomer());
		tvAddress.setText(o.getAddress());
		
		String phoneZip = "";
		if(o.getPhone().trim().length()>0){
			phoneZip += "#" + o.getPhone();
		}
		
		if(o.getZip().trim().length()>0){
			
			if(phoneZip.length()>0){
				phoneZip += ", " + o.getZip();
			}else{
				phoneZip += o.getZip();
			}
		}
		tvPhoneZip.setText(phoneZip);
		
		tvStatus.setText(o.getoStatus().getStatus());
		
		if(o.getDriverNote().trim().length()>0 || o.getAdressNote().trim().length() > 0 || o.getCusNote().trim().length() > 0 || o.getAdminNote().trim().length() > 0){
			tvNote.setBackgroundResource(R.drawable.btn_orange_5dp);
		} else {
			tvNote.setBackgroundResource(R.drawable.btn_gray_5dp);
		}
		
		if(position == 0){
			tvNew.setBackgroundResource(R.drawable.btn_blue_5dp);
		} else {
			tvNew.setBackgroundResource(R.drawable.btn_gray_5dp);
		}
		
		if(o.isVip()){
			tvVip.setBackgroundResource(R.drawable.btn_blue_5dp);
		} else {
			tvVip.setBackgroundResource(R.drawable.btn_gray_5dp);
		}

		int bagCount = o.getDryCleanCout() + o.getLaundryCount();
		tvBags.setText("Bags: "+bagCount);
		
		if (position % 2 == 0) { 
			convertView.setBackgroundResource(R.drawable.pressed_even_action);
		} else{
			convertView.setBackgroundResource(R.drawable.pressed_odd_action);
		} 

		return convertView;
	}

	public void setData(ArrayList<Order> items) {
		this.items = items;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Order getItem(int arg0) {
		return items.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
}
